package com.qst.rongxiaotong.controller;

import com.qst.rongxiaotong.common.Result;
import com.qst.rongxiaotong.pojo.DTO.UserDTO;
import com.qst.rongxiaotong.pojo.User;
import com.qst.rongxiaotong.properties.JwtProperties;
import com.qst.rongxiaotong.service.UserService;
import com.qst.rongxiaotong.utils.JwtClaimsConstant;
import com.qst.rongxiaotong.utils.JwtUtil;

import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

import lombok.extern.log4j.Log4j2;

/**
 * @author Ww
 * @version v1.0
 * @className UserController
 * @description
 * @date 2024/10/25 11:41
 **/
@RestController
@Log4j2
@RequestMapping("/user")
@CrossOrigin
@Api(tags = "用户登录")
public class UserController {

    @Autowired
    UserService userService;
    @Autowired
    private JwtProperties jwtProperties;



    @PostMapping("/login")
    public Result login(@RequestBody UserDTO userDTO){

        User user=userService.login(userDTO);
        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.USER_NAME, user.getUserName());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);
        log.info("token是:{}",token);

        return new Result(true,20000,"成功登录",token);
    }


}
